<template>
  <div class="app">
    <!-- 导航面包屑 -->
    <el-card>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>营销</el-breadcrumb-item>
        <el-breadcrumb-item :to="{path:'/home/packageMail'}">满包邮</el-breadcrumb-item>
        <el-breadcrumb-item>{{this.$store.state.marketing.action}}{{this.$store.state.marketing.addType}}</el-breadcrumb-item>
      </el-breadcrumb>
      <h1>{{this.$store.state.marketing.action}}{{this.$store.state.marketing.addType}}</h1>
    </el-card>

    <!-- 基础信息 -->
    <el-card>
      <el-row>
        <el-col :span="7">
          <p>基础信息</p>
        </el-col>
        <el-col :span="14">
          <el-form ref="addDiscountForm" :model="addPackageMailForm" label-width="80px">
            <!-- 占位 -->
            <el-form-item></el-form-item>
            <el-form-item></el-form-item>
            <!-- 活动名称 -->
            <el-form-item label="活动名称">
              <el-input style="width:350px" v-model="addPackageMailForm.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <!-- 活动日期 -->
            <el-form-item label="活动日期">
              <el-date-picker
                v-model="addPackageMailForm.validity"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
            <!-- 优惠叠加 -->
            <el-form-item label="优惠叠加">
              <el-radio-group v-model="addPackageMailForm.superposition ">
                <el-radio label="不可叠加"></el-radio>
                <el-radio label="可叠加其他类型优惠"></el-radio>
              </el-radio-group>
            </el-form-item>
            <!-- 适用商品 -->
            <el-form-item label="适用商品">
              <el-radio-group v-model="addPackageMailForm.goods">
                <el-radio label="全部商品"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-radio-group v-model="addPackageMailForm.goods">
                <el-radio label="部分商品"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="3"></el-col>
      </el-row>

      <!-- 提交和返回按钮 -->
      <el-button style="margin:30px 0 10px 410px" type="primary" @click="actionPackageMail">提交</el-button>
      <el-button @click="toPackageMail">返回</el-button>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addPackageMailForm: {
        name: "",
        type: "",
        validity: "",
        superposition: "",
        goods: "",
      },
      id: "",
    };
  },
  methods: {
    //点击跳转添加页面
    toPackageMail() {
      this.$router.push("/home/packageMail");
    },
    //添加满包邮
    async addPackageMail() {
      const { data: res } = await this.$axios.post("/vip/fullPackage/add", {
        name: this.addPackageMailForm.name,
        type: this.$store.state.marketing.addType,
        status: "1",
        validity: this.addPackageMailForm.validity[1],
        superposition: this.addPackageMailForm.superposition,
        goods: this.addPackageMailForm.goods,
      });
      if (res.code != 0) return this.$message.error("满包邮添加失败!");
      this.$message.success("满包邮添加成功!");
      this.toPackageMail();
    },
    //添加or修改
    actionPackageMail() {
      if (this.$store.state.marketing.action == "编辑")
        return this.editPackageMail();
      this.addPackageMail();
    },
    //修改满包邮
    async editPackageMail() {
      const { data: res } = await this.$axios.post("/vip/fullPackage/update", {
        name: this.addPackageMailForm.name,
        type: this.$store.state.marketing.addType,
        status: "1",
        validity: this.addPackageMailForm.validity[1],
        superposition: this.addPackageMailForm.superposition,
        goods: this.addPackageMailForm.goods,
        id: this.id,
      });
      if (res.code != 0) return this.$message.error("满包邮添加失败!");
      this.$message.success("满包邮添加成功!");
      this.toPackageMail();
    },
  },
  mounted() {
    if (!this.$store.state.marketing.action) return this.toPackageMail();
    //渲染修改信息
    if (this.$store.state.marketing.action == "编辑") {
      const info = this.$store.state.marketing.editInfo;
      this.addPackageMailForm.name = info.name;
      this.addPackageMailForm.validity = ["2021-08-01", info.validity];
      this.addPackageMailForm.superposition = info.superposition;
      this.addPackageMailForm.goods = info.goods;
      this.id = info.id;
    }
  },
};
</script>

<style>
</style>